package com.osig.ec.web;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.osig.ec.cfca.PaperlessConfig;

import cfca.paperless.client.bean.ChannelBean;
import cfca.paperless.client.bean.MultiDataBean;
import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.ImageUtil;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

public class htCompoundSealAutoPdf {

	public static void addDate(SealUserInfo sealUserInfo){
		String dateCode=GUID.generateId();
        String date=dateCode.substring(0,8);
        System.out.println(date);
        String businessCodeStyle = "font-size:15;x-ratio:0.12;y-ratio:0.97";  //流水的位置，大小
        sealUserInfo.setBusinessCode(date);
        sealUserInfo.setBusinessCodeStyle(businessCodeStyle);
	}
	
    public static void main(String[] args) throws Exception {

        try {
            PaperlessClient clientBean = new PaperlessClient("http://192.168.6.144:8080/PaperlessServer/PaperlessServlet");//http://192.168.6.144:8080/PaperlessServer/PaperlessServlet
            
            ///////////////////////////如下为加盖机构印章的策略/////////////////////////////////////////////////
            //SignLocation signLocation = new SignLocation(1,20, 20);
            String keywordb = "基金管理人：" + "恒天中岩投资管理有限公司" + "（盖章）";
			String locationStyleb = "R";
			int offsetXb = 0;
			int offsetYb = 0;
			SignLocation signLocationb = new SignLocation(keywordb, locationStyleb, offsetXb, offsetYb);
			// 初始化签章企业信息。由签章企业、签章地点、签章理由组成的签章企业信息
			String sealLocationb = "";
			String sealReasonb = "";
			SealUserInfo sealUserInfo = new SealUserInfo("恒天中岩投资管理有限公司", sealLocationb, sealReasonb);
			addDate(sealUserInfo);
            sealUserInfo.setSealImageSize(80);
            String sealCode = "yz2017112037102346";
            String sealPassword = "123456";
            String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, sealUserInfo, signLocationb);
            
            byte[] pdfFile = IoUtil.read(new FileInputStream("D://【基金合同】恒天财富稳裕1号私募投资基金.pdf"));
            String compoundSealStrategyXml = "<List>" /*+ proofSealStrategyXml1*//*+sealStrategyXml1*/ + sealStrategyXml +  "</List>";

            String bizTypeCode = "";//业务类型，比如：开户、开通网银
            PdfBean pdfBean = new PdfBean(Base64.toBase64String(pdfFile), bizTypeCode);
//            pdfBean = new PdfBean("", "a", bizTypeCode, "auto");
//            pdfBean.setInputFilePath("D:\\Tomcat\\SSL\\apache-tomcat-6.0.41\\webapps\\ROOT\\file.pdf");
            //PdfBean pdfBean = new PdfBean(Base64.toBase64String(pdfFile), bizTypeCode);
            String pdfBeanXml = PaperlessUtil.buildPdfBeanXml(pdfBean);
            long start = new Date().getTime();
            String channelBeanXml = new ChannelBean("00002").toString();
            //clientBean.setHttpClientWay(true);
            byte[] sealedPdf = clientBean.compoundSealAutoPdf(pdfBeanXml, ""/*multiData*/, compoundSealStrategyXml, "00001", channelBeanXml);

            System.out.println(sealedPdf.length);
            String errorRsString = PaperlessUtil.isError(sealedPdf);
            if ("".equals(errorRsString)) {
                IoUtil.write("D://191111.pdf", sealedPdf);
            } else {
                System.out.println(new String(sealedPdf, "UTF-8"));
            }
            System.out.println("########## compoundSealAutoPdf" + " end,time:" + (new Date().getTime() - start) + "##########");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
